package com.tianma315.web.open;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.core.common.exception.MessageException;
import com.tianma315.core.common.utils.Result;
import com.tianma315.core.product.domain.ProductDO;
import com.tianma315.core.product.service.ProductService;
import com.tianma315.core.trace.domain.TraceOutDO;
import com.tianma315.core.trace.service.TraceOutService;
import com.tianma315.core.trace.vo.TraceOutApiVO;
import com.tianma315.core.trace.vo.TraceOutVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/traceOut")
public class TraceOutRest  {

    @Autowired
    private TraceOutService traceOutService;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ProductService productService;

   /* @GetMapping("/traceOutInfo/{code}")
    public Result<TraceOutApiVO> traceOutInfo(@PathVariable("code") String code){
        TraceOutApiVO traceOutApiVO = traceOutService.getTraceOutInfo(code);
        return Result.ok(traceOutApiVO);
    }*/

    /**
     * 2020/06/09 by Lgc
     * @param code 溯源码
     * @return 带码跳转首页
     */
    @ResponseBody
    @GetMapping("/{code}")
    public ModelAndView traceOutInfos(@PathVariable("code") String code){
       ModelAndView modelAndView = new ModelAndView("trace/traceabilityFile/index");
        modelAndView.addObject("code",code);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping("/traceOutInfo/{code}")
    public ModelAndView codesResult(@PathVariable("code") String code){
        ModelAndView modelAndView = new ModelAndView("trace/traceabilityFile/codeResult");
        JSONObject result = null;
        try {
             result = traceOutService.getTraceOutInfo(code);
        }catch (MessageException me){
             if (me.getMessage().equals("该码不存在！")){
                 modelAndView.setViewName("error/not2");
             }else {
                 modelAndView.setViewName("error/not1");
             }
        }
        modelAndView.addObject("result",result);
        return modelAndView;
    }

    @GetMapping("/product/all")
    @ResponseBody
    public Result<List<ProductDO>> all(){
        List<ProductDO> productDOS = productService.getProductDOList();
        return Result.ok(productDOS);
    }

    @ResponseBody
    @GetMapping("/traceOut/list")
    public Result<Page<TraceOutVO>> list(@RequestParam("page") Integer pageNumber, @RequestParam("productId") Integer productId){
        // 查询列表数据
        Page<TraceOutVO> page = traceOutService.getTraceOutPage(pageNumber,10,productId);
        return Result.ok(page);
    }

    @GetMapping("/bolinxin")
    public String bolinxin(){
       return "trace/traceabilityFile/bolinxin";
    }

}
